/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.fei.ClassDiagram.drawing.Package;

import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.Collections;

/**
 *
 * @author Tomas
 */
public class PackageDrawing {

    private ArrayList<String> classes = null;
    private String name;
    
    private int x;
    private int y;
    
    private boolean isSorted;
    
    public Metrics m;
    
    public class Metrics {
        public int x;
        public int y;
        public int width;
        public int height;
        
        Metrics() {}
        Metrics(int x, int y, int w, int h) {
            this.x = x;
            this.y = y;
            this.width = w;
            this.height = h;
        }
    }
    
    public PackageDrawing(int x, int y, String name) {
        this.name = name;
        this.x = x;
        this.y = y;
        
        m = new Metrics(0, 0, 0, 0);
        isSorted = false;
    }
    
    public void draw(Graphics g) {
        if (isSorted == false) {
            Collections.sort(classes);
            isSorted = true;
        }
        if (name.equals("")) {
            m = this.drawClasses(x, y, g);
        }
        else {
            m = this.drawPackage(x, y, g);
        }
    }
    
    public Metrics drawPackage(int x, int y, Graphics g) {
        Metrics metrics = new Metrics();
        metrics.x = x;
        metrics.y = y;
        
        Font f = new Font(Font.SANS_SERIF, Font.BOLD, 12);
        g.setFont(f);
        
        FontMetrics fm = g.getFontMetrics();
        int maxWidth = fm.stringWidth(name);
        int maxHeight = fm.getAscent();
        g.drawString(name, x + 5, y + maxHeight);
        maxHeight = maxHeight + 5;
        g.drawRect(x, y, maxWidth + 10, maxHeight);
        int endName = maxHeight;
        int nameWidth = maxWidth;
        maxHeight = maxHeight*2;

        f = new Font(Font.SANS_SERIF, Font.PLAIN, 12);
        g.setFont(f);
        
        for (String s : classes) {
            g.drawString(s, x + 5, y + maxHeight);
            maxWidth = (maxWidth < fm.stringWidth(s)) ? fm.stringWidth(s) : maxWidth;
            maxHeight += fm.getAscent();
        }
        
        if (maxWidth <= nameWidth + 30)
            maxWidth = nameWidth + 30;
        else
            maxWidth += 10;
        
        g.drawRect(x, y + endName, maxWidth, maxHeight - endName);
        metrics.width = maxWidth;
        metrics.height = maxHeight;
        return metrics;
    }
    
    public Metrics drawClasses(int x, int y, Graphics g) {
        Metrics metrics = new Metrics();
        metrics.x = x;
        metrics.y = y;
        Font f = new Font(Font.SANS_SERIF, Font.PLAIN, 12);
        g.setFont(f);
        
        FontMetrics fm = g.getFontMetrics();
        
        int maxWidth = 0;
        int maxHeight = fm.getAscent();
        /* 
        g.drawString(name, x + 5, y + maxHeight);
        maxHeight = maxHeight + 5;
        g.drawRect(x, y, maxWidth + 10, maxHeight);
        int endName = maxHeight;
        maxHeight = maxHeight*2;
        maxWidth = 0;
        */

        for (String s : classes) {
            g.drawString(s, x + 5, y + maxHeight);
            maxWidth = (maxWidth < fm.stringWidth(s)) ? fm.stringWidth(s) : maxWidth;
            maxHeight += fm.getAscent();
        }
        
        maxWidth += 10;
        g.drawRect(x, y, maxWidth, maxHeight);
        metrics.width = maxWidth;
        metrics.height = maxHeight;
        return metrics;
    }
    
    public void addClass(String s) {
        if (this.classes == null)
            classes = new ArrayList<String>();
        
        classes.add(s);
    }
    
    public boolean isOver(int x, int y) {       
        if (x > this.x + m.width)
            return false;
        if (x < this.x)
            return false;
        if (y > this.y + m.height)
            return false;
        if (y < this.y)
            return false;
        return true; 
    }

    public String getName() {
        return name;
    }
    
    public int getWidth() {
        return m.width;
    }
    
    public int getHeight() {
        return m.height;
    }
    
    public int getX() {
        return this.x;
    }
    
    public int getY() {
        return this.y;
    }

    public void setX(int x) {
        this.x = x;
    }

    public void setY(int y) {
        this.y = y;
    }
}
